﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace Sys.Common
{
    /// <summary>
    /// Kind上传图片到临时文件夹
    /// </summary>
    public static class KindHelper
    {
        /// <summary>
        /// 上传图片到临时文件
        /// </summary>
        /// <returns>操作结果</returns>
        public static Hashtable ImgUpload()
        {
            HttpContext context = HttpContext.Current;

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("ext", "gif,jpg,jpeg,png,bmp");

            //最大文件大小
            int maxSize = Config.UploadMaxSize;

            HttpPostedFile imgFile = context.Request.Files["imgFile"];

            if (imgFile == null) { return GetKindError("请选择文件。"); }

            string fileName = imgFile.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
            {
                return GetKindError("上传文件大小超过限制。");
            }

            if (string.IsNullOrEmpty(fileExt) || !extTable["ext"].ToString().Contains(fileExt.Substring(1).ToLower()))
            {
                return GetKindError("上传文件扩展名是不允许的扩展名。\n只允许" + (extTable["ext"].ToString()) + "格式。");
            }

            string FileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;

            string filePath = context.Server.MapPath("~/" + Config.FileUploadPath + "/temp/");

            if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); }

            filePath = filePath + FileName;

            imgFile.SaveAs(filePath);

            Hashtable hash = new Hashtable();

            hash["error"] = 0;
            hash["url"] = FileName;

            return hash;
        }

        /// <summary>
        /// 上传图片到指定文件夹
        /// </summary>
        /// <param name="path">指定文件夹</param>
        /// <returns>操作结果</returns>
        public static Hashtable ImgUpload(string path)
        {
            HttpContext context = HttpContext.Current;

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("ext", "gif,jpg,jpeg,png,bmp");

            //最大文件大小
            int maxSize = Config.UploadMaxSize;

            HttpPostedFile imgFile = context.Request.Files["imgFile"];

            if (imgFile == null) { return GetKindError("请选择文件。"); }

            string fileName = imgFile.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
            {
                return GetKindError("上传文件大小超过限制。");
            }

            if (string.IsNullOrEmpty(fileExt) || !extTable["ext"].ToString().Contains(fileExt.Substring(1).ToLower()))
            {
                return GetKindError("上传文件扩展名是不允许的扩展名。\n只允许" + (extTable["ext"].ToString()) + "格式。");
            }

            string FileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;

            string filePath = context.Server.MapPath("~/" + Config.FileUploadPath + "/" + path + "/");

            if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); }

            filePath = filePath + FileName;

            imgFile.SaveAs(filePath);

            Hashtable hash = new Hashtable();

            hash["error"] = 0;
            hash["url"] = FileName;

            return hash;
        }

        /// <summary>
        /// 上传文件到指定文件夹
        /// </summary>
        /// <param name="path">指定文件夹</param>
        /// <returns>操作结果</returns>
        public static Hashtable FileUpload(string path)
        {
            HttpContext context = HttpContext.Current;

            //定义允许上传的文件扩展名
            Hashtable extTable = new Hashtable();
            extTable.Add("ext", "swf,flv,mp3,mp4,wav,wma,wmv,mid,avi,mpg,asf,rm,rmvb,doc,docx,xls,xlsx,ppt,htm,html,txt,zip,rar,gz,bz2");

            //最大文件大小
            int maxSize = Config.UploadMaxSize;

            HttpPostedFile imgFile = context.Request.Files["imgFile"];

            if (imgFile == null) { return GetKindError("请选择文件。"); }

            string fileName = imgFile.FileName;
            string fileExt = Path.GetExtension(fileName).ToLower();

            if (imgFile.InputStream == null || imgFile.InputStream.Length > maxSize)
            {
                return GetKindError("上传文件大小超过限制。");
            }

            if (string.IsNullOrEmpty(fileExt) || !extTable["ext"].ToString().Contains(fileExt.Substring(1).ToLower()))
            {
                return GetKindError("上传文件扩展名是不允许的扩展名。\n只允许" + (extTable["ext"].ToString()) + "格式。");
            }

            string FileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff", DateTimeFormatInfo.InvariantInfo) + fileExt;

            string filePath = context.Server.MapPath("~/" + Config.FileUploadPath + "/" + path + "/");

            if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); }

            filePath = filePath + FileName;

            imgFile.SaveAs(filePath);

            Hashtable hash = new Hashtable();

            hash["error"] = 0;
            hash["url"] = FileName;

            return hash;
        }

        /// <summary>
        /// 生成编辑器错误信息
        /// </summary>
        /// <param name="message">提示信息</param>
        /// <returns>结果</returns>
        public static Hashtable GetKindError(string message)
        {
            Hashtable error = new Hashtable();

            error["error"] = 1;
            error["message"] = message;

            return error;
        }
    }
}
